/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：呼叫中心-请求处理层-权限控制管理
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taiping.cc.authorize.webapi.controller;

import com.taiping.dlt.pojo.bo.AuthLoginBO;
import com.taiping.cc.authorize.webapi.handler.SecurityHandler;
import com.taiping.dlt.pojo.query.AuthLoginQO;
import com.taipingframework.boot.cache.constant.DurationConstant;
import com.taipingframework.boot.web.client.BrowserCookie;
import com.taipingframework.boot.web.client.ServletCookieScene;
import com.taipingframework.boot.web.response.result.ApiResult;
import com.taipingframework.boot.web.response.status.ApiStatusEnum;
import com.taipingframework.boot.web.support.ConfigHelper;
import com.taipingframework.utility.exception.ApplicationException;
import com.taipingframework.utility.thread.wrapper.BenchmarksFuture;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * 登录认证
 */
@RestController
@RequestMapping("/security")
@RequiredArgsConstructor
public class SecurityController {

    private final SecurityHandler securityHandler;

    /**
     * 判断当前会话是否处于登录状态
     */
    @PostMapping("/check")
    public CompletableFuture<ApiResult<Boolean>> check() {
        return securityHandler.check()
                .toCompletableFuture()
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                });
    }

    /**
     * 本地登录
     */
    @PostMapping("/login")
    public CompletableFuture<ApiResult<String>> login(@RequestBody AuthLoginQO loginQO) {
        return securityHandler.login(loginQO)
                .toCompletableFuture()
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                });
    }

    /**
     * 退出本地登录
     */
    @PostMapping("/logout")
    public CompletableFuture<ApiResult<Boolean>> logout() {
        return securityHandler.logout()
                .toCompletableFuture()
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                });
    }

    /**
     * 获取当前登录用户
     */
    @GetMapping("/current")
    public CompletableFuture<ApiResult<AuthLoginBO>> current() {
        return securityHandler.current()
                .toCompletableFuture()
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                });
    }

    /**
     * 配置信息加密
     */
    @GetMapping("/encrypt")
    public CompletableFuture<ApiResult<String>> configEncryptHandler(@RequestParam("value") String value) {
        return BenchmarksFuture.supplyAsync(() -> {
            // 读取cookie
            BrowserCookie cookie = ServletCookieScene.readCookie(value);
            String cookieValue = cookie.getValue();
            // cookie不存在
            if (StringUtils.isEmpty(cookieValue)) {
                cookieValue = ConfigHelper.encrypt(value);
                BrowserCookie browserCookie = new BrowserCookie(value, cookieValue, DurationConstant.MIN_3.instance());
                // 创建cookie
                ServletCookieScene.writeCookie(browserCookie);
            }

            return ApiResult.<String>build()
                    .setStatus(ApiStatusEnum.SUCCESS)
                    .setResult(cookieValue);
        }).toCompletableFuture().exceptionally(throwable -> {
            throw new ApplicationException(throwable.getCause());
        });
    }

}
